package hotel;

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.*;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import com.toedter.calendar.JDateChooser;

public class RicercaAvanzata extends JFrame implements ActionListener{
	private JTabbedPane tb;
	private JPanel p1,p2,pannelloTabella;
	private JDateChooser chooser1,chooser2;
	private JComboBox combo;
	private Calendar cal1,cal2,cal3;
	private JLabel dal,al;
	private JButton cerca1,cerca2;
	private MyDefaultTableModel modelClienti;
	private ResultSet rs;
	private JTable tabella,tabella2;
	private JScrollPane scroll;
	private GraficaHotel g;
	private JPanel pannello1;
	private ImageIcon iconae = new ImageIcon(
			"Risorse/iconae.png");
	private JScrollPane scroll2;
	private MyDefaultTableModel modelClienti1;
	private JLabel label,nomeL,cognomeL,dataDiNascitaL;
	private JPanel p3;
	private JPanel pannello3;
	private JTextField nome,cognome;
	private JDateChooser dataDiNascita;
	private JCheckBox boxNome,boxCognome,boxData;
	private JPanel mainPanel1;
	private JPanel pannello4;
	private JTable tabella3;
	private MyDefaultTableModel modelClienti3;
	private JPanel pannello5;
	private JComboBox comboBox;
	private String[] attributi={"Comune di Nascita","Comune di Residenza","Provincia di Nascita",
			"Provincia di Residenza","Email","Numero di Documento"};
	private JPanel mainPanel5;
	private JTextField ricerca;
	private JButton cerca3;
	private JTable tabella5;
	private MyDefaultTableModel modelClienti5;
	private JPanel mainPanel6;
	private JPanel pannello6;
	private JTextField ricercaNome;
	private JTextField ricerca6;
	private JButton cerca6;
	private JScrollPane scroll6;
	private JTable tabella6;
	private MyDefaultTableModel modelClienti6;
	private JButton delete;
	private ImageIcon iconai = new ImageIcon("Risorse/iconai.png");
	
	public RicercaAvanzata(GraficaHotel g){
		this.g=g;
		this.setSize(850,700);
		this.setResizable(false);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocation(250,20);
		this.setTitle("Ricerca Avanzata");
		tb=new JTabbedPane();
		Image immagineIc = Toolkit.getDefaultToolkit().getImage(
				"Risorse/ricerca1.png");
		this.setIconImage(immagineIc);

		mainPanel1=new JPanel(new BorderLayout());
		pannello3=new JPanel();
		boxNome=new JCheckBox("Nome");
		boxNome.addActionListener(this);
		boxCognome=new JCheckBox("Cognome");
		boxCognome.addActionListener(this);
		boxData=new JCheckBox("Data di nascita");
		boxData.addActionListener(this);
		boxNome.setSelected(true);
		boxCognome.setSelected(true);
		pannello3.add(boxNome);
		pannello3.add(boxCognome);
		pannello3.add(boxData);
		pannello4=new JPanel();
		nome=new JTextField(8);
		cognome=new JTextField(8);
		dataDiNascita=new JDateChooser();
		dataDiNascita.setDateFormatString("dd/MM/yyyy");
		dataDiNascita.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent e) {
				cal3 = dataDiNascita.getCalendar();
			}
		});
		nomeL=new JLabel("Nome");
		cognomeL=new JLabel("Cognome");
		dataDiNascitaL=new JLabel("Data di nascita");
		pannello4.add(nomeL);
		pannello4.add(nome);
		pannello4.add(cognomeL);
		pannello4.add(cognome);
		pannello4.add(dataDiNascitaL);
		pannello4.add(dataDiNascita);
		dataDiNascitaL.setVisible(false);
		dataDiNascita.setVisible(false);
		cerca2=new JButton("Cerca");
		cerca2.addActionListener(this);
		pannello4.add(cerca2);
		mainPanel1.add(pannello3,BorderLayout.NORTH);
		mainPanel1.add(pannello4,BorderLayout.SOUTH);
		creaTabellaRicClienti();
		scroll = new JScrollPane(tabella3);
		mainPanel1.add(scroll,BorderLayout.CENTER);
		tabella3.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					JTable target = (JTable) e.getSource();
					int row = target.getSelectedRow();
					try {
						new FinestraCliente((String) target.getValueAt(row, 2))
								.setVisible(true);
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		tb.add(mainPanel1,"Ricerca Cliente");
		
		
		pannello1=new JPanel(new BorderLayout());
		p1=new JPanel();
		pannelloTabella=new JPanel(new BorderLayout());
		dal=new JLabel("Dal:");
		p1.add(dal);
		chooser1=new JDateChooser();
		chooser1.setDateFormatString("dd/MM/yyyy");
		chooser1.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent e) {
				cal1 = chooser1.getCalendar();
			}
		});
		p1.add(chooser1);
		al=new JLabel("    Al:");
		p1.add(al);
		chooser2=new JDateChooser();
		chooser2.setDateFormatString("dd/MM/yyyy");
		chooser2.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent e) {
				cal2 = chooser2.getCalendar();
			}
		});
		p1.add(chooser2);
		cerca1=new JButton("Cerca");
		cerca1.addActionListener(this);
		p1.add(cerca1);
		tb.add(pannello1,"Ricerca per Range di Date");
		creaTabellaRange();
		scroll = new JScrollPane(tabella);
		pannelloTabella.add(scroll,BorderLayout.CENTER);
		pannello1.add(p1,BorderLayout.NORTH);
		pannello1.add(pannelloTabella,BorderLayout.CENTER);
		
		p2=new JPanel(new BorderLayout());
		combo=new JComboBox();
		combo.addActionListener(this);
		combo.addItem("--");
		riempiCombo();
		creaTabellaStanze();
		p2.add(combo,BorderLayout.NORTH);
		scroll2=new JScrollPane(tabella2);
		p2.add(scroll2,BorderLayout.CENTER);
		tb.add(p2,"Ricerca per camera");
		
		mainPanel5=new JPanel(new BorderLayout());
		pannello5=new JPanel();
		comboBox=new JComboBox(attributi);
		ricerca=new JTextField(10);
		cerca3=new JButton("Cerca");
		cerca3.addActionListener(this);
		pannello5.add(new JLabel("Ricerca:"));
		pannello5.add(ricerca);
		pannello5.add(comboBox);
		pannello5.add(cerca3);
		mainPanel5.add(pannello5,BorderLayout.NORTH);
		creaTabellAttributi();
		scroll2=new JScrollPane(tabella5);
		tabella5.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					JTable target = (JTable) e.getSource();
					int row = target.getSelectedRow();
					try {
						new FinestraCliente((String) target.getValueAt(row, 2))
								.setVisible(true);
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		mainPanel5.add(scroll2,BorderLayout.CENTER);
		tb.add(mainPanel5,"Ricerca Clienti Per Attributi");
		
		
		mainPanel6=new JPanel(new BorderLayout());
		pannello6=new JPanel();
		pannello6.add(new JLabel("A nome di:"));
		ricerca6=new JTextField(15);
		cerca6=new JButton("Cerca");
		cerca6.addActionListener(this);
		pannello6.add(ricerca6);
		pannello6.add(cerca6);
		mainPanel6.add(pannello6,BorderLayout.NORTH);
		creaTabellaPrenot();
		scroll6=new JScrollPane(tabella6);
		mainPanel6.add(scroll6,BorderLayout.CENTER);
		delete=new JButton("Cancella prenotazione");
		delete.addActionListener(this);
		mainPanel6.add(delete,BorderLayout.SOUTH);
		tb.add(mainPanel6,"Ricerca Prenotazioni");
		
		add(tb);
		this.setVisible(true);
	}
	
	private void creaTabellaPrenot() {
		try {
			modelClienti6 = new MyDefaultTableModel();
			rs = g.database.stat
					.executeQuery("SELECT camera,DATE_FORMAT(dal,'%d/%m/%Y'),DATE_FORMAT(al,'%d/%m/%Y'),nominativo from prenotazioni");
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			modelClienti6.addColumn("Camera");
			modelClienti6.addColumn("Dal");
			modelClienti6.addColumn("Al");
			modelClienti6.addColumn("Nominativo");
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
				}
				modelClienti6.addRow(row);
			}
			tabella6 = new JTable(modelClienti6);
			tabella6.setGridColor(Color.LIGHT_GRAY);
			tabella6.setVisible(true);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void creaTabellAttributi() {
		try {
			modelClienti5 = new MyDefaultTableModel();
			rs = g.database.stat
					.executeQuery("SELECT nome,cognome,numerodidocumento from clienti");
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			modelClienti5.addColumn("Nome");
			modelClienti5.addColumn("Cognome");
			modelClienti5.addColumn("Documento");
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
				}
				modelClienti5.addRow(row);
			}
			tabella5 = new JTable(modelClienti5);
			tabella5.setGridColor(Color.LIGHT_GRAY);
			tabella5.setVisible(true);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void riempiCombo() {
		try {
			rs=g.database.stat.executeQuery("Select numero from camere");
			while(rs.next())
				combo.addItem(rs.getString(1));
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void creaTabellaStanze() {
		try {
			modelClienti1 = new MyDefaultTableModel();
			rs = g.database.stat
					.executeQuery("SELECT nome,cognome,numcamera,DATE_FORMAT(dataarrivo,'%d/%m/%Y')," +
							"DATE_FORMAT(datapartenza,'%d/%m/%Y'),prezzo " +
							"from CLIENTI,STORICO where numerodidocumento=numerodocumento && numcamera='"+combo.getSelectedItem()+"'");
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			modelClienti1.addColumn("Nome");
			modelClienti1.addColumn("Cognome");
			modelClienti1.addColumn("Camera");
			modelClienti1.addColumn("Data di Arrivo");
			modelClienti1.addColumn("Data di Partenza");
			modelClienti1.addColumn("Prezzo");
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(3);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
				}
				modelClienti1.addRow(row);
			}
			tabella2 = new JTable(modelClienti1);
			tabella2.setGridColor(Color.LIGHT_GRAY);
			tabella2.setVisible(true);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void creaTabellaRicClienti() {
		try {
			modelClienti3 = new MyDefaultTableModel();
			rs = g.database.stat
					.executeQuery("SELECT nome,cognome,numerodidocumento from clienti");
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			modelClienti3.addColumn("Nome");
			modelClienti3.addColumn("Cognome");
			modelClienti3.addColumn("Documento");
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
				}
				modelClienti3.addRow(row);
			}
			tabella3 = new JTable(modelClienti3);
			tabella3.setGridColor(Color.LIGHT_GRAY);
			tabella3.setVisible(true);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void creaTabellaRange() {
		try {
			modelClienti = new MyDefaultTableModel();
			rs = g.database.stat
					.executeQuery("SELECT nome,cognome,numcamera,DATE_FORMAT(dataarrivo,'%d/%m/%Y')," +
							"DATE_FORMAT(datapartenza,'%d/%m/%Y'),prezzo " +
							"from CLIENTI,STORICO where numerodidocumento=numerodocumento");
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			modelClienti.addColumn("Nome");
			modelClienti.addColumn("Cognome");
			modelClienti.addColumn("Camera");
			modelClienti.addColumn("Data di Arrivo");
			modelClienti.addColumn("Data di Partenza");
			modelClienti.addColumn("Prezzo");
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(3);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
				}
				modelClienti.addRow(row);
			}
			tabella = new JTable(modelClienti);
			tabella.setGridColor(Color.LIGHT_GRAY);
			tabella.setVisible(true);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	
	class MyDefaultTableModel extends DefaultTableModel {

		private static final long serialVersionUID = 1L;

		public boolean isCellEditable(int row, int column) {
			return false;
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==cerca1){
			if(cal1!=null && cal2!=null){
				String anno1=""+cal1.get(Calendar.YEAR);
				String mese1=""+(cal1.get(Calendar.MONTH)+1);
				String giorno1=""+cal1.get(Calendar.DATE);
				if(mese1.length()==1)
					mese1="0"+mese1;
				if(giorno1.length()==1)
					giorno1="0"+giorno1;
				String anno2=""+cal2.get(Calendar.YEAR);
				String mese2=""+(cal2.get(Calendar.MONTH)+1);
				String giorno2=""+cal2.get(Calendar.DATE);
				if(mese2.length()==1)
					mese2="0"+mese2;
				if(giorno2.length()==1)
					giorno2="0"+giorno2;
				cercaPerRange(anno1+"/"+mese1+"/"+giorno1,anno2+"/"+mese2+"/"+giorno2);
				}else{
					JOptionPane.showMessageDialog(null,
						"Seleziona un range di date!","Avviso",JOptionPane.INFORMATION_MESSAGE,iconae);
					this.setVisible(true);
				}
			}else if(p2.isShowing() && e.getSource() instanceof JComboBox){
			cercaPerCamera();
		}else if(e.getSource()==boxNome){
			if(boxNome.isSelected()){
				nomeL.setVisible(true);
				nome.setVisible(true);
			}else{
				nomeL.setVisible(false);
				nome.setVisible(false);
			}
		}else if(e.getSource()==boxCognome){
			if(boxCognome.isSelected()){
				cognomeL.setVisible(true);
				cognome.setVisible(true);
			}else{
				cognomeL.setVisible(false);
				cognome.setVisible(false);
			}
		}else if(e.getSource()==boxData){
			if(boxData.isSelected()){
				dataDiNascitaL.setVisible(true);
				dataDiNascita.setVisible(true);
			}else{
				dataDiNascitaL.setVisible(false);
				dataDiNascita.setVisible(false);
			}
		}else if(e.getSource()==cerca2){
			if(boxNome.isSelected() && boxCognome.isSelected() && !boxData.isSelected()){
				cercaPerNomeCognome();
			}else if(boxNome.isSelected() && !boxCognome.isSelected() && !boxData.isSelected()){
				cercaPerNome();
			}else if(!boxNome.isSelected() && boxCognome.isSelected() && !boxData.isSelected()){
				cercaPerCognome();
			}else if(!boxNome.isSelected() && !boxCognome.isSelected() && boxData.isSelected()){
				cercaPerData();
			}else if(boxNome.isSelected() && boxCognome.isSelected() && boxData.isSelected()){
				cercaPerNomeCognomeData();
			}else if(!boxNome.isSelected() && boxCognome.isSelected() && boxData.isSelected()){
				cercaPerCognomeData();
			}else if(boxNome.isSelected() && !boxCognome.isSelected() && boxData.isSelected()){
				cercaPerNomeData();
			}
		}else if(e.getSource()==cerca3){
			if(comboBox.getSelectedItem().equals("Comune di Nascita")){
				cercaPerComuneDiNascita();
			}else if(comboBox.getSelectedItem().equals("Comune di Residenza")){
				cercaPerComuneDiResidenza();
			}else if(comboBox.getSelectedItem().equals("Provincia di Nascita")){
				cercaPerProvinciaDiNascita();
			}else if(comboBox.getSelectedItem().equals("Provincia di Residenza")){
				cercaPerProvinciaDiResidenza();
			}else if(comboBox.getSelectedItem().equals("Email")){
				cercaPerEmail();
			}else if(comboBox.getSelectedItem().equals("Numero di Documento")){
				cercaPerDocumento();
			}
		}else if(e.getSource()==cerca6){
			cercaPerNomePrenotato();
		}else if(e.getSource()==delete){
			int[] rows1 = tabella6.getSelectedRows();
			if (rows1.length < 1) {
				JOptionPane.showMessageDialog(null,
						"Nessuna prenotazione selezionata!", "Attenzione",
						JOptionPane.WARNING_MESSAGE, iconae);
				this.setVisible(true);
			} else if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(
					null, "Sicuro di voler continuare? Verranno eliminati i dati relativi alle prenotazioni selezionate", "Avviso",
					JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
					iconai)) {
				mergeSort(rows1, 0, rows1.length - 1);
				for (int i = 0; i < rows1.length; i++) {
					if (g.database.rimuoviPrenotazione((String) modelClienti6.getValueAt(
							rows1[i], 0),(String) modelClienti6.getValueAt(rows1[i],1),(String) modelClienti6.getValueAt(rows1[i],2))) {
						modelClienti6.removeRow(rows1[i]);
					}
				}
				this.setVisible(true);
			}
		}
	}
	
	public static void mergeSort(int[] v, int inf, int sup) {
		if (inf < sup) {
			int med = (inf + sup) / 2;
			mergeSort(v, inf, med);
			mergeSort(v, med + 1, sup);
			merge(v, inf, med, sup);
		}
	}

	private static void merge(int[] v, int inf, int med, int sup) {
		int[] aux = new int[sup - inf + 1];
		int i = inf, j = med + 1, k = 0;
		while (i <= med && j <= sup)
			if (v[i] > v[j]) {
				aux[k] = v[i];
				i++;
				k++;
			} else {
				aux[k] = v[j];
				j++;
				k++;
			}
		while (i <= med) {
			aux[k] = v[i];
			i++;
			k++;
		}
		while (j <= sup) {
			aux[k] = v[j];
			j++;
			k++;
		}
		for (k = 0; k < aux.length; k++)
			v[inf + k] = aux[k];
	}

	private void cercaPerNomePrenotato() {
		modelClienti6.setRowCount(0);
		try {
			String query="Select camera,DATE_FORMAT(dal,'%d/%m/%Y'),DATE_FORMAT(al,'%d/%m/%Y'),nominativo from prenotazioni where nominativo like '%"+ricerca6.getText()+"%'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti6.addRow(row);
			}
			tabella6.setModel(modelClienti6);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerDocumento() {
		modelClienti5.setRowCount(0);
		try {
			String query="Select nome,cognome,numerodidocumento from clienti where numerodidocumento='"+ricerca.getText()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti5.addRow(row);
			}
			tabella5.setModel(modelClienti5);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerEmail() {
		modelClienti5.setRowCount(0);
		try {
			String query="Select nome,cognome,numerodidocumento from clienti where email='"+ricerca.getText()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti5.addRow(row);
			}
			tabella5.setModel(modelClienti5);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerProvinciaDiResidenza() {
		modelClienti5.setRowCount(0);
		try {
			String query="Select nome,cognome,numerodidocumento from clienti where provinciadiresidenza='"+ricerca.getText()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti5.addRow(row);
			}
			tabella5.setModel(modelClienti5);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerProvinciaDiNascita() {
		modelClienti5.setRowCount(0);
		try {
			String query="Select nome,cognome,numerodidocumento from clienti where provinciadinascita='"+ricerca.getText()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti5.addRow(row);
			}
			tabella5.setModel(modelClienti5);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerComuneDiResidenza() {
		modelClienti5.setRowCount(0);
		try {
			String query="Select nome,cognome,numerodidocumento from clienti where residenza='"+ricerca.getText()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti5.addRow(row);
			}
			tabella5.setModel(modelClienti5);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerComuneDiNascita() {
		modelClienti5.setRowCount(0);
		try {
			String query="Select nome,cognome,numerodidocumento from clienti where comunedinascita='"+ricerca.getText()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti5.addRow(row);
			}
			tabella5.setModel(modelClienti5);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerNomeData() {
		if(cal3==null){
			return;
		}
		modelClienti3.setRowCount(0);
		try {
			String dat=cal3.get(Calendar.YEAR)+"/"+(cal3.get(Calendar.MONTH)+1)+"/"+cal3.get(Calendar.DATE);
			String query="Select nome,cognome,numerodidocumento from clienti where datadinascita='"+dat+"' && nome='"+nome.getText()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti3.addRow(row);
			}
			tabella3.setModel(modelClienti3);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerCognomeData() {
		if(cal3==null){
			return;
		}
		modelClienti3.setRowCount(0);
		try {
			String dat=cal3.get(Calendar.YEAR)+"/"+(cal3.get(Calendar.MONTH)+1)+"/"+cal3.get(Calendar.DATE);
			String query="Select nome,cognome,numerodidocumento from clienti where datadinascita='"+dat+"' && cognome='"+cognome.getText()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti3.addRow(row);
			}
			tabella3.setModel(modelClienti3);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerNomeCognomeData() {
		if(cal3==null){
			return;
		}
		modelClienti3.setRowCount(0);
		try {
			String dat=cal3.get(Calendar.YEAR)+"/"+(cal3.get(Calendar.MONTH)+1)+"/"+cal3.get(Calendar.DATE);
			String query="Select nome,cognome,numerodidocumento from clienti where datadinascita='"+dat+"' && nome='"+nome.getText()+"' && cognome='"+cognome.getText()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti3.addRow(row);
			}
			tabella3.setModel(modelClienti3);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerData() {
		if(cal3==null){
			return;
		}
		modelClienti3.setRowCount(0);
		try {
			String dat=cal3.get(Calendar.YEAR)+"/"+(cal3.get(Calendar.MONTH)+1)+"/"+cal3.get(Calendar.DATE);
			String query="Select nome,cognome,numerodidocumento from clienti where datadinascita='"+dat+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti3.addRow(row);
			}
			tabella3.setModel(modelClienti3);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerCognome() {
		modelClienti3.setRowCount(0);
		try {
			String query="Select nome,cognome,numerodidocumento from clienti where cognome='"+cognome.getText()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti3.addRow(row);
			}
			tabella3.setModel(modelClienti3);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerNome() {
		modelClienti3.setRowCount(0);
		try {
			String query="Select nome,cognome,numerodidocumento from clienti where nome='"+nome.getText()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti3.addRow(row);
			}
			tabella3.setModel(modelClienti3);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerNomeCognome() {
		modelClienti3.setRowCount(0);
		try {
			String query="Select nome,cognome,numerodidocumento from clienti where nome='"+nome.getText()+"' && cognome='"+cognome.getText()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti3.addRow(row);
			}
			tabella3.setModel(modelClienti3);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerCamera() {
		modelClienti1.setRowCount(0);
		try {
			String query="SELECT c.nome,c.cognome,s.numcamera,DATE_FORMAT(s.dataarrivo,'%d/%m/%Y')," +
					"DATE_FORMAT(s.datapartenza,'%d/%m/%Y'),s.prezzo " +
					"from CLIENTI c,STORICO s where c.numerodidocumento=s.numerodocumento && numcamera='"+combo.getSelectedItem()+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti1.addRow(row);
			}
			tabella2.setModel(modelClienti1);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void cercaPerRange(String data1,String data2) {
		modelClienti.setRowCount(0);
		try {
			String query="SELECT c.nome,c.cognome,s.numcamera,DATE_FORMAT(s.dataarrivo,'%d/%m/%Y')," +
					"DATE_FORMAT(s.datapartenza,'%d/%m/%Y'),s.prezzo " +
					"from CLIENTI c,STORICO s where c.numerodidocumento=s.numerodocumento && dataarrivo between '"+data1+"' and '"+data2+"'";
			rs = GraficaHotel.database.stat.executeQuery(query);
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			Vector<String> row;
			while (rs.next()) {
				row = new Vector<String>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					row.add(rs.getString(i));
					}
				modelClienti.addRow(row);
			}
			tabella.setModel(modelClienti);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


}

